<?php

	session_start();
	
	function PricerrTheme_filter_ttl($title){return __("Edit job",'PricerrTheme')." - ";}
	add_filter( 'wp_title', 'PricerrTheme_filter_ttl', 10, 3 );	
	
	if(!is_user_logged_in()) { wp_redirect(get_bloginfo('siteurl')."/wp-login.php"); exit; }   
	   
	global $current_user, $wp_query;
	get_currentuserinfo;   

	$pid = $_GET['jobid'];
	$post = get_post($pid);

	$uid 	= $current_user->ID;
	$title 	= $post->post_title;
	$cid 	= $current_user->ID;
	
	if($uid != $post->post_author) { echo 'Not your post. Sorry!'; exit; }

//-------------------------------------

		if(isset($_POST['save-job']))
		{
			
			//extra job stuff
			
			for($k=1;$k<=3;$k++)
			{
				$extra_price 	= trim($_POST['extra'.$k.'_price']);
				$extra_content 	= trim($_POST['extra'.$k.'_content']);
				
				
				if(!empty($extra_price) && is_numeric($extra_price) && !empty($extra_content)):
				
					update_post_meta($pid, 'extra'.$k.'_price', 	$extra_price);
					update_post_meta($pid, 'extra'.$k.'_content', 	$extra_content);
				
				else:
						
					update_post_meta($pid, 'extra'.$k.'_price', 	'');
					update_post_meta($pid, 'extra'.$k.'_content', 	'');	
						
				endif;
			}
		
			//-----------------------------
			
			$job_title 			= trim(strip_tags(htmlspecialchars($_POST['job_title'])));
			$job_description 	= trim(nl2br(strip_tags(htmlspecialchars($_POST['job_description']))));
			$job_tags 			= trim(strip_tags(htmlspecialchars($_POST['job_tags'])));	
				
			$max_days = trim(strip_tags(htmlspecialchars($_POST['max_days'])));
			$job_cost = htmlspecialchars(trim($_POST['job_cost']));
			$instruction_box	= substr( nl2br(strip_tags(htmlspecialchars($_POST['instruction_box']))), 0 , 500);
			update_post_meta($pid, "instruction_box", $instruction_box);
			
		//-----------------------	
			
			$PricerrTheme_enable_dropdown_values 	= get_option('PricerrTheme_enable_dropdown_values');
            $PricerrTheme_enable_free_input_box 	= get_option('PricerrTheme_enable_free_input_box');
		
			if($PricerrTheme_enable_dropdown_values == "yes" || $PricerrTheme_enable_free_input_box == "yes")
			update_post_meta($pid, "price", $job_cost);
			else		
			update_post_meta($pid, "price", get_option('PricerrTheme_job_fixed_amount'));
			
		//-----------------------
			
			$my_post 					= array();
			$my_post['ID'] 				= $pid;
			$my_post['post_content'] 	= $job_description;
			$my_post['post_title'] 		= $job_title;
			

	
			wp_update_post( $my_post );
				
			$term 		= get_term( $_POST['job_cat_cat'], 'job_cat' );	
			$job_cat 	= $term->slug;
			
			wp_set_post_tags( $pid, $job_tags);
			wp_set_object_terms($pid, array($job_cat),'job_cat');

			update_post_meta($pid, "max_days", $max_days);			
			update_post_meta($pid, "has_video", "0");
			update_post_meta($pid, "shipping", trim($_POST['shipping']));
			
			for($i=1;$i<=3;$i++){
				
				$y_link = htmlspecialchars($_POST['youtube_link'.$i]);
				update_post_meta($pid, "youtube_link".$i, trim($y_link));
				update_post_meta($pid, "has_video", "1");
			
			}
			
			$job_saved = 1;
			
			/**************************************************************/
			
			update_post_meta($pid, 'featured',	"0");	
			$featured = get_post_meta($pid, 'featured',	true);
			$paid	  = get_post_meta($pid, 'paid',		true);
			
			if($_POST['featured'] == "1")
			{
				if($paid != "1")
				{
					$my_post = array();
					$my_post['post_status'] 	= 'draft';
					$my_post['ID'] 				= $pid;		
					wp_update_post( $my_post );
					
					update_post_meta($pid, 'featured',	"1");
					
					$using_permalinks = PricerrTheme_using_permalinks();
				
					if($using_permalinks) $rdrlnk = get_permalink(get_option('PricerrTheme_pay_for_posting_job_page_id'))."?jobid=".$pid;
					else $rdrlnk = get_bloginfo('siteurl')."/?page_id=".get_option('PricerrTheme_pay_for_posting_job_page_id')."&jobid=".$pid;
					
					wp_redirect($rdrlnk);
				}
				else
				{
					update_post_meta($pid, 'featured',	"1");	
				}
			}	
			
			
		}
		
		
					
		$price 		= get_post_meta($pid, 'price', true);
		$ttl		= $post->post_title;
		$max_days 	= get_post_meta($pid, "max_days", true);
		$location 	= wp_get_object_terms($pid, 'job_location');
		$cat 		= wp_get_object_terms($pid, 'job_cat');
		
		
		
		
		
			$post = get_post($pid);
			get_header();
		
			?>
            
	
        <div id="content">
        	<div class="padd10">
       
            
            	<div class="box_title"><?php echo sprintf(__("Edit Job - %s", 'PricerrTheme'), $post->post_title); ?></div>
            	<div class="box_content">
              
              <?php
				
					if($job_saved == 1):
					
						echo '<div class="edit-job-ok"><div class="padd10">'.__('Your job has been saved.','PricerrTheme').'</div></div>';
					
					endif;
				
				
				?>
                
              
              
               <ul class="post-new">
 
   <form method="post">    
        <li>
                                <h2><?php echo __('I will', 'PricerrTheme'); ?>:</h2>
                                <p><input type="text" size="40" class="do_input" name="job_title" 
                                value="<?php echo $post->post_title; ?>" /> <span class="large_font"><?php _e("for","PricerrTheme"); ?> 
                                <?php 
                            
							$PricerrTheme_enable_dropdown_values 	= get_option('PricerrTheme_enable_dropdown_values');
                            $PricerrTheme_enable_free_input_box 	= get_option('PricerrTheme_enable_free_input_box');
                            
                            if($PricerrTheme_enable_free_input_box == "yes")
                            {
                                
                                if(PricerrTheme_show_price_in_front() == true)
                                echo PricerrTheme_get_currency();
                                    
                                echo ' <input type="text" name="job_cost" class="do_input" value="'.$price.'" size="5" /> ';
                                
                                if(PricerrTheme_show_price_in_front() == false)
                                echo PricerrTheme_get_currency();
                                
                            }
                            elseif($PricerrTheme_enable_dropdown_values == "yes")
                            echo PricerrTheme_get_variale_cost_dropdown('do_input', $price);
                            else		
                            echo PricerrTheme_get_show_price(get_option('PricerrTheme_job_fixed_amount'));
                            
                            
                             ?></span> </p>
                            </li> 
        
        <li>
        	<h2><?php echo __('Category', 'PricerrTheme'); ?>:</h2>
        	<p><?php	echo PricerrTheme_get_categories("job_cat",  
			!isset($_POST['job_cat_cat']) ? (is_array($cat) ? $cat[0]->term_id : "") : htmlspecialchars($_POST['job_cat_cat'])
			, __('Select Category','PricerrTheme'), "do_input"); ?></p>
        </li>
        
        
         <li>
        	<h2><?php echo __('Location', 'PricerrTheme'); ?>:</h2>
        	<p><?php	echo PricerrTheme_get_categories("job_location",  
			!isset($_POST['job_location_cat']) ? (is_array($location) ? $location[0]->term_id : "") : htmlspecialchars($_POST['job_location_cat'])
			, __('Select Location','PricerrTheme'), "do_input"); ?></p>
        </li>
        
 
        <li>
        	<h2><?php echo __('Description', 'PricerrTheme'); ?>:</h2>
        <p><textarea rows="6" cols="45" class="do_input"  name="job_description"><?php 
		echo empty($_POST['job_description']) ? trim($post->post_content) : htmlspecialchars($_POST['job_description']); ?></textarea><br/>
       <?php _e('Min: 100 chars. Max: 500','PricerrTheme'); ?></p>
        </li>


	 <?php $instruction_box = get_post_meta($post->ID, 'instruction_box', true); ?>
                            
                             <li><h2><?php echo __('Instructions to buyer', 'PricerrTheme'); ?>:</h2>
                            <p><textarea rows="6" cols="45" class="do_input"  name="instruction_box"><?php 
                            echo empty($_POST['instruction_box']) ? trim($instruction_box) : htmlspecialchars($_POST['instruction_box']); ?></textarea></p>
                            </li>
                            
                    
                    		<li>

<?php

	$job_tags = '';
	$t = wp_get_post_tags($post->ID);
	
	foreach($t as $tag)
	{
		$job_tags .= $tag->name.',';
	}
?>
		<li>
        	<h2><?php echo __('Tags', 'PricerrTheme'); ?>:</h2>
        <p><input type="text" size="50" class="do_input"  name="job_tags" value="<?php echo $job_tags; ?>" /> </p>
        </li>
        
        
        <?php
							
								$PricerrTheme_enable_shipping = get_option('PricerrTheme_enable_shipping');
								if($PricerrTheme_enable_shipping == "yes"):
								
							?>
                            
                            <li>
                                <h2><?php echo __('Requires shipping?', 'PricerrTheme'); ?>:</h2>
                            <p>
                            <?php if(PricerrTheme_show_price_in_front())
                                echo PricerrTheme_get_currency(); ?>
                            <input type="text" size="5" class="do_input"  name="shipping" value="<?php echo (empty($shipping) ? get_post_meta($pid,'shipping',true) : $shipping ); ?>" />
                            <?php if(!PricerrTheme_show_price_in_front())
                                echo PricerrTheme_get_currency(); ?> </p>
                            </li>
                            
                            <?php endif; ?>
        
        <li>
        	<h2><?php echo __('Max Days do Deliver', 'PricerrTheme'); ?>:</h2>
        <p><input type="text" size="10" class="do_input"  name="max_days" value="<?php echo $max_days; ?>" /> </p>
        </li>
        
        
         <li>
        <h2><?php echo __('Images', 'PricerrTheme'); ?>:</h2>
        
<?php        
        	global $current_user;
	get_currentuserinfo();
	$cid = $current_user->ID;

	$cwd = str_replace('wp-admin','',getcwd());

	$cwd .= 'wp-content/uploads';

	//echo get_template_directory();
?>



	<style>
	.div_div
	{
		margin-left:5px; float:left; 
		width:110px;margin-top:10px;
	}
	
	</style>
	<link rel="stylesheet" href="<?php echo get_bloginfo('template_url'); ?>/lib/uploadify/uploadify.css" type="text/css" />


	
	<script type="text/javascript" src="<?php echo get_bloginfo('template_url'); ?>/js/swfobject.js"></script>     
	<script type="text/javascript" src="<?php echo get_bloginfo('template_url'); ?>/js/jquery.uploadify.v2.1.4.min.js"></script>


	<script type="text/javascript">

	var $ = jQuery;
	
	function delete_this(id)
	{
		 $.ajax({
						method: 'get',
						url : '<?php echo get_bloginfo('siteurl');?>/index.php/?_ad_delete_pid='+id,
						dataType : 'text',
						success: function (text) {   $('#image_ss'+id).remove();  }
					 });
		  //alert("a");
	
	}


$(function() {

	$("#fileUpload3").uploadify({
		'uploader': '<?php echo get_bloginfo('template_url'); ?>/lib/uploadify/uploadify.swf',
		'cancelImg': '<?php echo get_bloginfo('template_url'); ?>/lib/uploadify/cancel.png',
		'script': '<?php echo get_bloginfo('siteurl'); ?>/wp-content/themes/<?php echo get_template(); ?>/lib/uploadify/uploady.php',
		'folder': '<?php echo str_replace("\\","/" ,$cwd); ?>/',
		'fileDesc': '<?php echo __('Image Files','PricerrTheme'); ?>',
		'fileExt': '*.jpg;*.jpeg;*.gif;*.png',
		'scriptData'  : {'ID':<?php echo $pid; ?>,'author':<?php echo $cid; ?>},
		'multi': true,
		'buttonText': '<?php echo __('Select Images','PricerrTheme'); ?>',
		'auto': true,
		onComplete : function(event,queueID,fileObj,response,data) {
			
			
			var bar = response.split("|");
			
$('#thumbnails').append('<div class="div_div" id="image_ss'+bar[1]+'" ><img width="70" class="image_class" height="70" src="' + bar[0] + '" /><a href="javascript: void(0)" onclick="delete_this('+ bar[1] +')"><img border="0" src="<?php echo get_bloginfo('template_url'); ?>/images/delete_icon.png" border="0" /></a></div>');
}
	});
});

</script>


	<div id="fileUpload3">You have a problem with your javascript</div>
	<div id="thumbnails" style="overflow:hidden;margin-top:20px">
    
    <?php


	$args = array(
	'order'          => 'ASC',
	'orderby'        => 'menu_order',
	'post_type'      => 'attachment',
	'post_parent'    => $post->ID,
	'post_mime_type' => 'image',
	'post_status'    => null,
	'numberposts'    => -1,
	);
	$attachments = get_posts($args);
	if ($attachments) {
	    foreach ($attachments as $attachment) {
		$url = wp_get_attachment_url($attachment->ID);
			echo '<div class="div_div"  id="image_ss'.$attachment->ID.'"><img width="70" class="image_class" height="70" src="' .
			PricerrTheme_generate_thumb($url, 70, 70). '" />
			<a href="javascript: void(0)" onclick="delete_this(\''.$attachment->ID.'\')"><img border="0" src="'.get_bloginfo('template_url').'/images/delete_icon.png" /></a>
			</div>';
	  
	}
	}


	?>
    
    </div>

      
        </li>
       
       
        <li>
        	<h2><?php echo __('Youtube Video Link #1','PricerrTheme'); ?>:</h2>
        <p><input type="text" size="50" name="youtube_link1" class="do_input" 
        	value="<?php echo get_post_meta($pid, 'youtube_link1', true); ?>" /></p>
        </li>
        
   		
         <li>
        	<h2><?php echo __('Youtube Video Link #2','PricerrTheme'); ?>:</h2>
        <p><input type="text" size="50" name="youtube_link2" class="do_input" 
        	value="<?php echo get_post_meta($pid, 'youtube_link2', true); ?>" /></p>
        </li>
        
        
         <li>
        	<h2><?php echo __('Youtube Video Link #3','PricerrTheme'); ?>:</h2>
        <p><input type="text" size="50" name="youtube_link3" class="do_input" 
        	value="<?php echo get_post_meta($pid, 'youtube_link3', true); ?>" /></p>
        </li>
        
    
       
          
        <?php $featured = get_post_meta($pid, 'featured', true); ?>
    <li>
        <h2>
       <?php _e("Feature job?",'PricerrTheme'); ?>:</h2>
        <p><input type="checkbox" class="do_input" name="featured" value="1" <?php if($featured == "1") echo 'checked="checked"'; ?> /> 
        <?php _e("By clicking this checkbox you mark your job as featured. Extra fee is applied.", 'PricerrTheme'); ?></p>
        </li>
     
        <?php
							
			$PricerrTheme_enable_extra = get_option('PricerrTheme_enable_extra');
			if($PricerrTheme_enable_extra != "no"):
							
							?>
        
        <li class="xtra_stuff"><div class="padd10">
        <table width="100%">
        <?php
		
			for($i=1;$i<=3;$i++):
		
		?>
            <tr><td valign="top">
            <?php _e('For an extra','PricerrTheme'); ?> 
            
			<?php if(PricerrTheme_show_price_in_front())  echo PricerrTheme_get_currency(); ?>
            <input type="text" size="3" name="extra<?php echo $i; ?>_price" value="<?php echo get_post_meta($pid, 'extra'.$i.'_price', true); ?>" />
			<?php if(!PricerrTheme_show_price_in_front())  echo PricerrTheme_get_currency(); ?>
            
            &nbsp; &nbsp; <?php _e('I will:','PricerrTheme'); ?> </td>
            <td>  <textarea name="extra<?php echo $i; ?>_content" cols="40" rows="2"><?php echo get_post_meta($pid, 'extra'.$i.'_content', true); ?></textarea></td></tr>
    
        <?php endfor; ?>
        
        </table>
        
      	</div>
        </li>
        <?php endif; ?>
        
        <li>
        <h2>&nbsp;</h2>
        <p><input type="submit" name="save-job" value="<?php _e("Save Job", 'PricerrTheme'); ?> >>" /></p>
        </li>
    
    </form>
    </ul>

              
              
                
                </div>
               
        
        </div></div>
            

	<?php PricerrTheme_get_users_links(); ?>


		<?php
		get_footer();
		?>